package com.wanchuang.qhbb.module.machines;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.Mystock;
import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.qhbb.utils.StatusBarUtil;
import com.wanchuang.qhbb.view.machines.FirstNode;
import com.wanchuang.qhbb.view.machines.NodeSectionAdapter;
import com.wanchuang.qhbb.view.machines.SecondNode;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MachinesActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.btn_hua_back)
    Button btnHuaBack;
    @BindView(R.id.btn_hua)
    Button btnHua;



    @BindView(R.id.tv_ge_ren_dai)
    TextView tv_ge_ren_dai;

    @BindView(R.id.tv_ge_ren_yi)
    TextView tv_ge_ren_yi;




    private int position;
    private NodeSectionAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machines);
        StatusBarUtil.setWhiteStatusBar(this);
        ButterKnife.bind(this);
        tvTitle.setText("我的机具");
        tvTitleRight.setText("出入库记录");
        initRecycle();
        initListener();
        loadData();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());
        btnHuaBack.setOnClickListener(v -> {
            startActivity(AllowBackActivity.class);
        });
        btnHua.setOnClickListener(v -> {
            startActivity(AllowActivity.class);
        });
        tvTitleRight.setOnClickListener(v -> {
            startActivity(ChuRuRecordActivity.class);
        });
    }

    private void loadData() {
        NetworkManager.INSTANCE.post(Apis.myMachineGcTypeList, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mystock.myMachineGcTypeList result = Mystock.myMachineGcTypeList.parseFrom(response);
                Log.e("wang", "onOk: " + result.toString());
                List<Mystock.myMachineGcTypeList.goodsInfo> list = result.getDesListList();
                List<Mystock.myMachineGcTypeList.tabs> tabsList = result.getGoodsTabListList();

                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getGoodsSerial()).append("\n");
                }
                des.append(list.get(list.size() - 1).getGoodsSerial());


                Log.e("get_modify_policy_logs", result.toString());
//                tv_ge_ren.setText(result.getStockNum());
//                tv_ge_ren_wei.setText(result.getUnbindNum());
                tv_ge_ren_dai.setText(result.getStockNum());
                tv_ge_ren_yi.setText(result.getTeamStockNum());



                //总的 list，里面放的是 FirstNode
                List<BaseNode> listNode = new ArrayList<>();
                position = 0;
                for (int i = 0; i < tabsList.size(); i++) {
                    //SecondNode 的 list
                    List<BaseNode> secondNodeList = new ArrayList<>();
                    // 切割
                    List<Mystock.myMachineGcTypeList.goodsInfo> splits = tabsList.get(i).getGoodsInfoListList();
                    for (int n = 0; n < splits.size(); n++) {
                        SecondNode seNode = new SecondNode(splits.get(n));
                        seNode.setTitle( tabsList.get(i).getGoodsTab());
                        secondNodeList.add(seNode);
                    }
                    FirstNode entity = new FirstNode(secondNodeList, tabsList.get(i).getGoodsTab(), i);
                    listNode.add(entity);
                }
                adapter.addData(listNode);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                adapter.setUseEmpty(true);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();

    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        adapter = new com.wanchuang.qhbb.view.machines.NodeSectionAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        // 先注册需要点击的子控件id（注意，请不要写在convert方法里）
        adapter.addChildClickViewIds(R.id.ll_action, R.id.ll_un_action,R.id.ll_on_bine);
        adapter.setEmptyView(R.layout.recycle_empty);
        // 设置子控件点击监听
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {

            SecondNode data = (SecondNode) view.getTag();
            Mystock.myMachineGcTypeList.goodsInfo goodsInfo =  data.getGoodsInfo();
            String  titles =  data.getTitle();
            if (view.getId() == R.id.ll_un_action) {
                Intent intent = new Intent(mContext, MachinesNextActivity.class);
                intent.putExtra("title", "未激活 " +titles + " "
                        + goodsInfo.getGoodsSerial());
                intent.setType(goodsInfo.getGoodsCommonid());
                intent.setAction("3"); // 未激活
                startActivity(intent);
            } else if (view.getId() == R.id.ll_action) {
                Intent intent1 = new Intent(mContext, MachinesNextActivity.class);
                intent1.putExtra("title", "激活 " + titles + " "
                        + goodsInfo.getGoodsSerial());
                intent1.setType(goodsInfo.getGoodsCommonid());
                intent1.setAction("2"); // 激活
                startActivity(intent1);
            } else if (view.getId() == R.id.ll_on_bine) {
                Intent intent1 = new Intent(mContext, MachinesNextActivity.class);
                intent1.putExtra("title", "未绑定 " + titles + " "
                        + goodsInfo.getGoodsSerial());
                intent1.setType(goodsInfo.getGoodsCommonid());
                intent1.setAction("1"); // 未绑定
                startActivity(intent1);
            }
        });
    }

}
